<template>
  <div class="login container">
    <Header>
      <!-- <span>注册</span> -->
    </Header>
    <section>
      <div class="login-tel">
        <input
          type="text"
          v-model="userTel"
          placeholder="请输入手机号"
          pattern="[0-9]*"
        />
      </div>
      <div class="login-code">
        <input type="text" v-model="userPwd" placeholder="请输入密码" />
      </div>
      <div class="login-btn" @click="login">登入</div>
      <div class="tab">
        <span @click="goLogin">短信登入</span>
        <span @click="goRecovery">找回密码</span>
        <span @click="goRegister">快速注册</span>
      </div>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import http from "@/common/api/request";
import { Toast } from "mint-ui";
import Header from "./HeadeR.vue";
import Tabbar from "@/components/common/TabbaR.vue";
export default {
  data() {
    return {
      userTel: "", //用户输入的手机号
      userPwd: "", //用户输入的密码
      //验证规则
      rules: {
        userTel: {
          //手机号验证
          rule: /^1[23456789]\d{9}$/,
          msg: "手机号不能为空,并且是11位数字",
        },
        //密码验证
        userPwd: {
          rule: /^\w{6,12}$/,
          msg: "密码不能为空,并且要求6,12位",
        },
      },
    };
  },
  components: {
    Tabbar,
    Header,
  },
  methods: {
    ...mapMutations(['USER_LOGIN']),
    //点击登入
    login() {
      //前端验证
      if (!this.validate("userTel")) return;
      if (!this.validate("userPwd")) return;
      //发送请求 后端验证
      http
        .$axios({
          url: "/api/login",
          method: "POST",
          data: {
            userTel: this.userTel,
            userPwd: this.userPwd,
          },
        })
        .then((res) => {
          //提示信息
          Toast(res.msg);
          //登录失败
          if (!res.success) return;

          //登录成功==》跳转页面存储信息
          this.USER_LOGIN( res.data );
          //跳转到我的
          this.$router.push('/MyS')
        });
    },
    //注册
    goRegister() {
      this.$router.push({
        path: "/register",
      });
    },
    //找回密码
    goRecovery(){
        this.$router.push({
        path: "/recovery",
      });
    },
    //验证信息
    goLogin() {
      this.$router.push({
        path: "/login",
      });
    },
    //验证信息
    validate(key) {
      let bool = true;
      if (!this.rules[key].rule.test(this[key])) {
        //提示信息
        Toast(this.rules[key].msg);
        bool = false;
        return false;
      }
      return bool;
    },
  },
};
</script>

<style scoped lang="scss">
section {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #f5f5f5;
  div {
    margin: 0.2666rem 0;
    width: 8.93333rem;
    height: 1.17333rem;
    font-size: 0.32rem;
  }
  input {
    box-sizing: border-box;
    padding: 0 0.26666rem;
    border: 1px solid #ccc;
    border-radius: 6px;
    background-color: #ffffff;
    line-height: 1.17333rem;
  }
  .login-tel {
    margin-top: 0.8rem;
    input {
      width: 8.93333rem;
    }
  }
  .login-code {
    display: flex;
    input {
      flex: 1;
    }
    button {
      line-height: 1.17333rem;
      background-color: #b0352f;
      padding: 0 0.53333rem;
      border: 0;
      border-radius: 6px;
      color: #fff;
    }
  }
  .login-btn {
    text-align: center;
    line-height: 44px;
    color: #fff;
    border-radius: 6px;
    background-color: #b0352f;
    font-size: 0.48rem;
  }
  .tab {
    display: flex;
    justify-content: space-between;
    font-size: 0.37333rem;
  }
}
</style>